<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.sdry.model.llm.User"%>
<%@page import="java.util.Date"%>
<%@page import="java.text.SimpleDateFormat"%>
<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>留样管理</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<style type="text/css">
		 .left-label {
		 	width: 100px !important
		 }
	</style>
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/layui.css"  media="all">
	<link rel="stylesheet" href="${pageContext.request.contextPath }/assets/css/toastr.min.css"  media="all">
	<%
		User people = (User) request.getSession().getAttribute("currentUser");
	    String name = null;
	    if(people != null){
	      name = people.getUserName();
	    }
  	%>
  	<%
	  	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	  	String nowDate = dateFormat.format(new Date());
  	%>
  	<script type="text/javascript">
	  	//获取当前时间
	  	var date = new Date();
	  	var year = date.getFullYear();
	  	var month = date.getMonth() + 1;
	  	var day = date.getDate();
	  	if (month < 10) {
	  	    month = "0" + month;
	  	}
	  	if (day < 10) {
	  	    day = "0" + day;
	  	}
	  	var nowDate = year + "-" + month + "-" + day;
  	</script>
</head>
<body>
	<!-- <table style="border: 1px solid blue">
		<tr>
			<td><button class="layui-btn layui-btn-blue" onclick="getMedia()"><i class="layui-icon layui-icon-delete">开启摄像头</i></button></td>
			<td><button class="layui-btn layui-btn-blue" onclick="takePhoto()"><i class="layui-icon layui-icon-delete">拍照</i></button></td>
			<td><button type="button" class="layui-btn" id="file" name="file"><i class="layui-icon">&#xe67c;</i>上传图片</button></td>
			<td style="width: 200px; height:150px"><video id="video" autoplay="autoplay" style="width:100%; height:100%; object-fit: fill;border: 1px solid red;"></video></td>
			<td style="width: 200px; height:150px"><canvas id="canvas" style="border: 1px solid red;width:100%; height:100%;"></canvas></td>
		</tr>
	</table> -->
	<div class="demoTable" style="padding:10px 0 0 15px">
    <div class="layui-inline">
    	<%--<input class="layui-input" name="searchTime" id="searchTime" placeholder="请输入时间检索" style="display: inline; width: 45%">--%>
        <input class="layui-input" name="keyword" id="keyword" placeholder="请输入样品名称" autocomplete="off" style="display: inline; width: 100%">
    </div>
    <button class="layui-btn layui-btn-blue" style="margin: 0 0 0 10px" data-type="reload"><i class="layui-icon layui-icon-search">搜索</i></button>
</div>
<table class="layui-hide" id="tableList" lay-filter="tableList"></table>
<div id="addDivID" hidden="hidden">
  	<form class="layui-form" id="addFormID" enctype="multipart/form-data">
		<br>
		<table>
			<tr>
				<td>&emsp;&emsp;&emsp;&emsp;留样人&emsp;</td>
				<td><input class="layui-input" id="samplekeepMan" name="samplekeepMan" value="<%= name%>" lay-verify="samplekeepMan" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
				<td>&emsp;&emsp;&emsp;&emsp;留样日期&emsp;</td>
				<td><input class="layui-input" id="samplekeepTime" name="samplekeepTime" value="<%= nowDate%>" lay-verify="samplekeepTime" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
			</tr>
			<tr style="height:10px"></tr>
			<tr>
				<td>&emsp;&emsp;&emsp;&emsp;留样地点&emsp;</td>
				<td><input class="layui-input" id="samplekeepLocation" name="samplekeepLocation" lay-verify="samplekeepLocation" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
				<td>&emsp;&emsp;&emsp;&emsp;样品名称&emsp;</td>
				<td><input class="layui-input" id="sampleName" name="sampleName" lay-verify="sampleName" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
			</tr>
			<tr style="height:10px"></tr>
			<tr>
				<td>&emsp;&emsp;&emsp;&emsp;样品数量&emsp;</td>
				<td><input class="layui-input" id="sampleCount" name="sampleCount" lay-verify="sampleCount" style="width:150px; display: inline;" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onafterpaste="this.value=this.value.replace(/[^\d]/g,'')"/>
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
				<!-- <td><input class="layui-input" id="sampleCount" name="sampleCount" lay-verify="sampleCount" style="width:150px"></td> -->
				<td>&emsp;&emsp;&emsp;&emsp;样品编号&emsp;</td>
				<td><input class="layui-input" id="sampleNum" name="sampleNum" lay-verify="sampleNum" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
			</tr>
			<tr style="height:10px"></tr>
			<tr>
				<td>&emsp;&emsp;&emsp;&emsp;检测结果&emsp;</td>
				<td><input class="layui-input" id="detectionResult" name="detectionResult" lay-verify="detectionResult" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
				<td>&emsp;&emsp;&emsp;&emsp;失效日期&emsp;</td>
				<td><input class="layui-input" id="invalidTime" name="invalidTime" lay-verify="invalidTime" style="width:150px; display: inline;">
					<div class="layui-inline" style="margin-top: 5px;">
						<font style="color:red; font-size: 24px; margin-left: 4px">*</font>
					</div>
				</td>
			</tr>
			<tr style="height:10px"></tr>
			<tr>
				<td>&emsp;&emsp;&emsp;&emsp;上传图片&emsp;</td>
				<td><button type="button" class="layui-btn" id="file" name="file" style="width: 150px"><i class="layui-icon">&#xe67c;</i>上传图片</button><input class="layui-hide" id="uploadAttachment" name="uploadAttachment"></td>
				<td>&emsp;&emsp;&emsp;&emsp;备注&emsp;</td>
				<td><input class="layui-input" id="remark" name="remark" lay-verify="remark" style="width:150px"></td>
			</tr>
			<tr style="height:30px"></tr>
			<tr>
				<td colspan="2"><button class="layui-btn layui-btn-blue" id="subBtn" lay-submit lay-filter="addForm" style="margin-left:160px; margin-bottom: 20px">提交</button></td>
				<td colspan="2">&emsp;&emsp;&emsp;&emsp;<button type="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px">重置</button></td>
			</tr>
		</table>
	</form>
</div>
<script type="text/html" id="toolbar">
	<div class="layui-btn-container">
		<button class="layui-btn layui-btn-blue" lay-event="add"><i class="layui-icon layui-icon-add-1">新增</i></button>
		<button class="layui-btn layui-btn-blue" lay-event="delete"><i class="layui-icon layui-icon-delete">批量删除</i></button>
	</div>
</script>

<script type="text/html" id="rowToolbar">
	<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
	<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script src="${pageContext.request.contextPath }/assets/layui.js" charset="utf-8"></script>
<%-- <script src="${pageContext.request.contextPath }/assets/layui.all.js" charset="utf-8"></script> --%>
<script src="${pageContext.request.contextPath }/assets/js/jquery.min.js" charset="utf-8"></script>
<script src="${pageContext.request.contextPath }/assets/js/toastr.min.js" charset="utf-8"></script>

<script type="text/javascript">
	layui.use(['table','layer','upload','form','laydate'], function(){
		var table = layui.table;
		var layer = layui.layer;
		var laydate = layui.laydate;
	    var form = layui.form;
	    var upload = layui.upload;
		var $ = layui.jquery, active = {
			reload:function () {
				/*var searchTime = $("#searchTime").val();*/
				var keyword01 = $("#keyword").val();
				/*console.log(searchTime + ',' + keyword)*/
				table.reload('contenttable',{
					method:'get',
                    where:{"keyword01":keyword01},
					page: {
						curr: 1
					}
				});
			}
		}
		$('.layui-btn').on('click', function(){
	        var type = $(this).data('type');
	        active[type] ? active[type].call(this) : '';
	    });

		table.render({
		    elem: '#tableList'
		    ,url:'${pageContext.request.contextPath }/samplekeep/querySamplekeep.do'
		    ,toolbar: '#toolbar'
		    ,title: '设备台帐信息'
		    ,id :'contenttable'
		    ,limits:[10,20,30]
		    ,done:function(res, curr, count) { //表格数据加载完后的事件
	            //调用示例
	            layer.photos({//点击图片弹出
	                photos: '.layer-photos-demo'
	                ,anim: 1 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
	            	,area: ['800px','600px']
	            });
	        }
		    ,cols: [[
		      {type: 'checkbox', fixed: 'left'},
		      {field:'', title:'序号', sort: true, type:'numbers', width: 60},
		      {field:'samplekeepMan', title:'留样人'},
		      {field:'samplekeepTime', title:'留样日期'},
		      {field:'samplekeepLocation', title:'留样地点'},
		      {field:'sampleName', title:'样品名称'},
		      {field:'sampleNum', title:'样品编号'},
		      {field:'sampleCount', title:'样品数量'},
		      {field:'detectionResult', title:'检测结果'},
		      {field:'invalidTime', title:'失效日期'},
		      {field:'uploadAttachment', title:'查看附件', templet:function (rowData){
		    	  if(rowData.uploadAttachment == ''){
          				return "无图片";
		           	}else{
		           	 	return "<div class='layer-photos-demo' id='imgview'><img layer-pid='pid1' layer-src='${pageContext.request.contextPath }/attachment/samplekeep" + rowData.uploadAttachment + "' src='${pageContext.request.contextPath }/attachment/samplekeep" + rowData.uploadAttachment + "' alt='查看附件' style='height:35px;'/></div>"
		           	}
		      }},
		      {field:'remark', title:'备注'},
		      {fixed: 'right', title:'操作', toolbar: '#rowToolbar', width: 150}
		    ]],
		    page: true
		});

		//头工具栏事件
		table.on('toolbar(tableList)', function(obj){
			var checkStatus = table.checkStatus(obj.config.id);
		    switch(obj.event){
		    	case 'add':
			    	layer.open({
						type: 1 				//Page层类型
				  		,area: ['610px', '410px'] 	//宽  高
				  		,title: '新增'
				  		,shade: 0.6 			//遮罩透明度
				  		,maxmin: true 			//允许全屏最小化
				  		,anim: 1 				//0-6的动画形式，-1不开启
				  		,content: $("#addDivID")
				  		,end: function(){
				  			$("#addDivID").hide();
				  		}
						,success: function(){
							//留样时间
							laydate.render({
							    elem: '#samplekeepTime'
							 });
							//失效日期
							laydate.render({
								elem: '#invalidTime'
							});
			  			}
				});
		      	break;
                //批量删除
		    	case 'delete':
		        	var data = checkStatus.data;
		        	var idArr = new Array();
		        	if(data.length==0){
		        		toastr.warning("请至少选择一条记录！");
		        	}
		        	for(var i=0;i<data.length;i++){
                        idArr[i] = data[i].id;
		        	}
                    $.ajax({
                        type:'post',
                        url:'${pageContext.request.contextPath }/samplekeep/deleteSamplekeepById.do',
                        data:{"idArr" : idArr},
                        success:function(data){
                            layer.confirm('确定删除吗？', function(index){
                            	if(data > 0){
                                    toastrStyle();
                                    toastr.success("删除成功！");
                                    setTimeout(function(){
                                        location.reload();
                                    },1000);
                                }else{
                                    toastrStyle();
                                    toastr.warning("删除失败！");
                                    setTimeout(function(){
                                        location.reload();
                                    },1000);
                                }
                            });
                        }
                    });
		      	break;
		      	case 'camera':
		      		/*console.log(obj)*/
		      	break;
		    };
		});

		//监听行工具事件
		table.on('tool(tableList)', function(obj){
			var data = obj.data;
            var idArr = new Array();
			/*console.log(obj)*/
			//单个删除
			if(obj.event === 'del'){
			  layer.confirm('确定删除吗？', function(index){
			    var id = obj.data.id;
			    idArr[0] = id;
			    $.ajax({
			    	type:'post',
			    	url:'${pageContext.request.contextPath }/samplekeep/deleteSamplekeepById.do',
                    data:{ "idArr" : idArr },
			    	success:function(data){
                        if(data > 0){
                            toastrStyle();
                            toastr.success("删除成功！");
                            setTimeout(function(){
                                location.reload();
                            },1000);
                        }else{
                            toastrStyle();
                            toastr.warning("删除失败！");
                            setTimeout(function(){
                                location.reload();
                            },1000);
                        }
			    	}
			    })
			    layer.close(index);
			  });
			} else if(obj.event === 'edit'){
				layer.open({
				  type: 1 					          //Page层类型
				  ,area: ['610px', '410px']     //宽  高
				  ,title: '编辑'
				  ,shade: 0.6 				        //遮罩透明度
				  ,maxmin: true 			        //允许全屏最小化
				  ,anim: 1 					        //0-6的动画形式，-1不开启
				  ,content: '<form class="layui-form" id="editFormID">'+
				  	'<table>'+
				  		'<tr>'+
							'<td><input class="layui-hide" id="id" name="id" value="'+data.id+'"></td>'+
						'<tr>'+
                        '<tr style="height:20px"></tr>'+
                        '<tr>'+
							'<td>&emsp;&emsp;&emsp;&emsp;留样人&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="samplekeepMan" name="samplekeepMan" value="'+data.samplekeepMan+'" lay-verify="samplekeepMan" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
							'<td>&emsp;&emsp;&emsp;&emsp;留样日期&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="samplekeepTime1" name="samplekeepTime" value="'+data.samplekeepTime+'" lay-verify="samplekeepTime" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
						'</tr>'+
						'<tr style="height:10px"></tr>'+
						'<tr>'+
							'<td>&emsp;&emsp;&emsp;&emsp;留样地点&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="samplekeepLocation" name="samplekeepLocation" value="'+data.samplekeepLocation+'" lay-verify="samplekeepLocation" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
							'<td>&emsp;&emsp;&emsp;&emsp;样品名称&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="sampleName" name="sampleName" value="'+data.sampleName+'" lay-verify="sampleName" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
						'</tr>'+
						'<tr style="height:10px"></tr>'+
						'<tr>'+
							'<td>&emsp;&emsp;&emsp;&emsp;样品数量&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="sampleCount" name="sampleCount" value="'+data.sampleCount+'" lay-verify="sampleCount" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
							'<td>&emsp;&emsp;&emsp;&emsp;样品编号&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="sampleNum" name="sampleNum" value="'+data.sampleNum+'" lay-verify="sampleNum" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
						'</tr>'+
						'<tr style="height:10px"></tr>'+
						'<tr>'+
							'<td>&emsp;&emsp;&emsp;&emsp;检测结果&emsp;</td>'+
							'<td>'+
								'<input class="layui-input" id="detectionResult" name="detectionResult" value="'+data.detectionResult+'" lay-verify="detectionResult" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
							'<td>&emsp;&emsp;&emsp;&emsp;失效日期&emsp;</td>'+
							'<td>'+
								'<input class="layui-input"id="invalidTime1" name="invalidTime" value="'+data.invalidTime+'" lay-verify="invalidTime" style="width:150px; display: inline;">'+
								'<div class="layui-inline" style="margin-top: 5px;">'+
									'<font style="color:red; font-size: 24px; margin-left: 4px">*</font>'+
								'</div>'+
							'</td>'+
						'</tr>'+
						'<tr style="height:10px"></tr>'+
						'<tr>'+
							/*
							'<td>&emsp;&emsp;&emsp;&emsp;上传图片&emsp;</td>'+
							'<td><button type="button" class="layui-btn" id="file" name="file"><i class="layui-icon">&#xe67c;</i>上传图片</button><input class="layui-hide" id="uploadAttachment" name="uploadAttachment" type="text" ></td>'+
							*/
							'<td>&emsp;&emsp;&emsp;&emsp;备注&emsp;</td>'+
							'<td><input class="layui-input" id="remark" name="remark" value="'+data.remark+'" lay-verify="remark" style="width:150px"></td>'+
							'<td></td>'+
                        	'<td></td>'+
						'</tr>'+
						'<tr style="height:30px"></tr>'+
						'<tr align="center">'+
							'<td colspan="2"><button class="layui-btn layui-btn-blue" lay-submit lay-filter="editForm" style="margin-left:160px; margin-bottom: 20px">提交</button>'+
							'<td colspan="2">&emsp;&emsp;&emsp;&emsp;<button type="reset" class="layui-btn layui-btn-primary" style="margin-bottom: 20px">重置</button>'+
						'<tr>'+
					'</table>'+
					'</form>'
				,success: function(){
					//留样日期
					laydate.render({
					    elem: '#samplekeepTime1'
					 });
					//失效日期
					laydate.render({
						elem: '#invalidTime1'
					});
				}
			});
			}
		});

        /**
         * 通用表单提交(AJAX方式)(新增)
         */
        form.on('submit(addForm)', function (data) {
        	$("#subBtn").attr("disabled",true);
            $.ajax({
                url : '${pageContext.request.contextPath}/samplekeep/addSamplekeep.do',
                data: $("#addFormID").serialize(),
                cache : false,
                type : "post",
            }).done(
                function(res) {
                    if (res > 0) {
                        toastrStyle();
                        toastr.success('新增成功！');
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }
                }
            ).fail(
                function(res) {
                    toastrStyle();
                    toastr.error('新增失败！');
                    setTimeout(function(){
                        location.reload();
                    },1000);
                }
            )
            return false;
        });
        /**
         * 通用表单提交(AJAX方式)(修改)
         */
        form.on('submit(editForm)', function (data) {
            $.ajax({
                url : '${pageContext.request.contextPath}/samplekeep/editSamplekeepById.do',
                data: $("#editFormID").serialize(),
                cache : false,
                type : "post",
            }).done(
                function(res) {
                    if (res > 0) {
                        toastrStyle();
                        toastr.success('修改成功！');
                        setTimeout(function(){
                            location.reload();
                        },1000);
                    }
                }
            ).fail(
                function(res) {
                    toastrStyle();
                    toastr.error('修改失败！');
                    setTimeout(function(){
                        location.reload();
                    },1000);
                }
            )
            return false;
        });

		/**
	     * 新增表单校验
	     */
	    form.verify({
	    	//value：表单的值item：表单的DOM对象
            samplekeepMan: function(value, item){
	    		if(value == ''){
	    			return '留样人不能为空';
	    		}
	    	},
            samplekeepTime: function(value, item){
	    		if(value == ''){
	    			return '留样日期不能为空';
	    		}
	    	},
            samplekeepLocation: function(value, item){
                if(value == ''){
                    return '留样地点不能为空';
                }
            },
            sampleName: function(value, item){
                if(value == ''){
                    return '样品名称不能为空';
                }
            },
            sampleCount: function(value, item){
                if(value == ''){
                    return '样品数量不能为空';
                }
            },
            sampleNum: function(value, item){
                if(value == ''){
                    return '样品编号不能为空';
                }
            },
            detectionResult: function(value, item){
                if(value == ''){
                    return '检测结果不能为空';
                }
            },
            invalidTime: function(value, item){
                if(value == ''){
                    return '失效日期不能为空';
                }
            }
	    });

	  	//查找带回
        form.on('select(displayInfo)', function(data){
        	$.ajax({
                type: 'POST',
                url: '${pageContext.request.contextPath}/device/queryDeviceInfoByNo.do',
                data: {deviceNo:$('#deviceNo').val()},
                /* dataType: 'json', */
                /* async: false, */
                success: function (data){
                	var device = eval('(' +data+ ')');
                	$('#deviceName').val(device.deviceName);
                	$('#deviceVersion').val(device.deviceVersion);
                	$('#manufacturersNo').val(device.deviceNo);
                	$('#deviceConfig').val(device.deviceConfig);
                	$('#deviceTotalPower').val(device.devicePower);
                	$('#buyTime').val(device.buyTime);
                	$('#useTime').val(device.useTime);
                	$('#supplier').val(device.manufacturers);
                	$('#location').val(device.location);
                	$('#useDept').val(device.useDept);
                	$('#useMan').val(device.useMan);
                }
            });
        });

      	//多文件上传
        upload.render({
          elem: '#file'
          ,url: '${pageContext.request.contextPath}/upload/picUpload.do'
          ,multiple: true
          //允许上传的文件类型
          ,exts: 'jpg|png|gif|jpeg|PNG|zip|rar|txt|doc|docx|xls|xlsx|ppt|pptx|mp3|mp4|avi|flv|rm|rmvb|'
          /* ,before: function(obj){
            //预读本地文件示例，不支持ie8
			obj.preview(function(index, file, result){
              	$('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img">')
            });
          } */
          ,done: function(res){
            //上传完毕
       	  if(res != "" && res != null){
	      		toastrStyle();
	      		toastr.success('文件上传成功！');
	      		//$("#viewFiles").attr('href','http://localhost:8000//attachment//samplekeep'+res);
		      	$("#uploadAttachment").val(res);
	      	}else{
	      		toastrStyle();
	      		toastr.warning('图片上传失败！');
	      	}
          }
        });
	});
	toastr.options.positionClass = 'toast-top-center'; //提示框的位置设置为中上
    //提示框样式
    function toastrStyle(){
        toastr.options = {
            "closeButton": false,
            "debug": false,
            "newestOnTop": false,
            "progressBar": false,
            "positionClass": "toast-top-center",
            "preventDuplicates": false,
            "onclick": null,
            "showDuration": "5000",
            "hideDuration": "1000",
            "timeOut&quot": "100000",
            "extendedTimeOut": "0",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        };
    }

	//查看附件
	function lookupAttachment(rowId){
		$.ajax({
            type: 'POST',
            url: '${pageContext.request.contextPath}/samplekeep/queryFilesURLById.do',
            data: {id:rowId},
            /* dataType: 'json', */
            /* async: false, */
            success: function (url){
            	//alert(url);
            	$("#viewFiles").attr('href','http://localhost:8000//attachment//samplekeep'+url);
            }
        });
	}

	//开启摄像头
	function getMedia() {
        let constraints = {
            video: {width: 200, height: 150},
            audio: true
        };
        let promise = navigator.mediaDevices.getUserMedia(constraints);
        promise.then(function (MediaStream) {
            video.srcObject = MediaStream;
            video.play();
        });
	}

	//拍照上传
	function takePhoto() {
     	//获得Canvas对象
     	let video = document.getElementById("video");
     	let canvas = document.getElementById("canvas");
     	let ctx = canvas.getContext('2d');
     	//在画布坐标（0,0）的位置绘制一张宽度为300，高度为200的图片
     	ctx.drawImage(video, 0, 0, 300, 150);
     	var timestamp = Date.parse(new Date());
     	downloadCanvasIamge('canvas', timestamp);
	}

	// 下载Canvas元素获取的图片
	function downloadCanvasIamge(selector, name) {
	    // 通过选择器获取canvas元素
	    var canvas = document.querySelector(selector);
	    var url = canvas.toDataURL('image/png');
	    var a = document.createElement('a');
	    var event = new MouseEvent('click');
	    a.download = name || Date.parse(new Date());
	    a.href = url;
	    // 触发a的单击事件
	    a.dispatchEvent(event);
	    closeCamera();
	}

	function closeCamera(){
		/* this.mediaStreamTrack && this.mediaStreamTrack.stop(); */
		/* this.mediaStreamTrack.stop(); */
		/* stream.getTracks()[0].stop(); */
		/* window.location.reload(); */
	}

</script>

</body>
</html>